package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LifeServlet",urlPatterns = "/life")
public class LifeServlet extends HttpServlet {
    private int value;
    public void init() throws ServletException {
        super.init();
        System.out.println("LifeServlet.init");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=uft-8");

        synchronized (this){//单例类，可以使用this
            System.out.println(this.hashCode());
            String method = request.getParameter("method");
            if("add".equals(method)){
                value++;

            }else if("minus".equals(method)){
                value--;
            }
            response.getWriter().println("当前值：="+value);

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
    }
}
